import * as moment from 'moment';
import checkInService from './checkInService';
import dTalkApi from '../core/dTalkApi';
import visitMonthPlanModel from '../model/visitMonthPlanModel';
import consumerRecoderModel from '../model/consumerRecoderModel';
import formDataService from './formDataService';
import dTalkApiUtil from 'dingtalk-app/dtalk/dTalkApiUtil';
import emplModel from '../model/empl2Model';
const schedule = require("node-schedule");
class SyncDataService {
    constructor() { }
    async run() {
        //定时获取每天员工签到的数据
        let date5 = moment().format('YYYY-MM-DD');
        let j = await schedule.scheduleJob('00 00 22 * * *', function (date5: string) {
             let json = checkInService.saveUserCheckinData(date5);
        }); 
    }
    //根据领导id查询其名下的所有的销售(id与名字)
    async getUserListByLeaderId(leaderId: string) {
        console.log(moment().format('YYYY-MM-DD HH:mm:ss'))
        let empData: any[] = [];
        let userIdData: any[] = []
        let userNames: string[] = []   
        let access_token=await dTalkApiUtil.getAccessToken();
        let leaderData=await formDataService.getAuthrity(leaderId,access_token)
        let leaderDepts = leaderData.leaderDepts;
        let empArr=await emplModel.distinct('userId',{})
        if(leaderDepts.length>0){
            for (let index = 0; index < leaderDepts.length; index++) {
                let dept = leaderDepts[index];
                let sonData = await dTalkApi.getSondepIdList(dept, true,access_token)
                sonData.department.push(parseInt(dept));//
                let empList=await visitMonthPlanModel.distinct("userId",{"$and":[{userId:{$in:empArr}},{deptId:{$in:sonData.department}}]})
                for (let key2 in empList) {
                    if (empList.hasOwnProperty(key2)) {
                        let element2 = empList[key2];
                        if(!userIdData.includes(element2)){
                            userIdData.push(element2)
                        }
                    }
                }
            }
            if(userIdData.length>0){
                for (let num2 = 0; num2 < userIdData.length; num2++) {
                    let id = userIdData[num2];
                    let empinfo=await emplModel.findOne({userId:id,"$and":[{userName:{$ne:''}},{userName:{$ne:null}}]});
                    if(empinfo){
                        userNames.push(empinfo.userName)
                        empData.push({
                            userId: id,
                            userName: empinfo.userName
                        })
                    }
                }
            }
            console.log(moment().format('YYYY-MM-DD HH:mm:ss'))
            return {  userIdList: userIdData, userNames: userNames ,empData:empData}
        }
        return null;
    }
    
    //根据领导获取其名下的所有员工负责的客户的所在城市名称
    async getCityNameBelongToLeader(userId:any){
        let data: any[] = [];
        let access_token=await dTalkApiUtil.getAccessToken();
        let user=await dTalkApi.getUserDetail(userId,access_token)
        let isLeaderInDepts: string = user.isLeaderInDepts || ''
        isLeaderInDepts = isLeaderInDepts.substr(1, isLeaderInDepts.length - 2)
        let arrayLeaderDepts = isLeaderInDepts.split(',')
        let leaderDepts = []
        arrayLeaderDepts.map(item => {
            if (item.endsWith(":true")) {
                leaderDepts.push(item.substr(0, item.length - ":true".length))
            }
        })
        let dataCache: any = []
        for (let index = 0; index < leaderDepts.length; index++) {
            let dept = leaderDepts[index]
            let sonData = await dTalkApi.getSondepIdList(dept, true,access_token);
            let son_list = sonData.department;
            son_list.push(parseInt(dept));
            let userIdLsit=await visitMonthPlanModel.distinct("userId",{deptId:{$in:son_list}});
           dataCache=await consumerRecoderModel.distinct("city",{comment:1,userid:{$in:userIdLsit}});
        }
        for (const key in dataCache) {
            if (dataCache.hasOwnProperty(key)) {
                const element = dataCache[key];
                data.push(element)
            }
        }
        return data;
    }

    
} 









let syncData = new SyncDataService();
export default syncData;
